﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using System.Linq;
using System.Data.Linq.SqlClient;

public partial class SreachResul : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        ShopDataContext dc = new ShopDataContext();
        if (!IsPostBack)
        {
            var Cate = from c in dc.Categories
                        where c.cActive == true
                        select c;
            foreach (var item in Cate)
            {
                ddlCate.Items.Add(new ListItem(item.cName, item.cId.ToString()));
            }


            if (Request.QueryString["type"] != null)
            {
                if (Request.QueryString["type"].Equals("0"))
                {

                    if (Request.QueryString["pName"] != null && Request.QueryString["cId"] != null)
                    {
                        string cId1 = Request.QueryString["cId"];
                        string pCode = Request.QueryString["pCode"];
                        string pName = Request.QueryString["pName"];
                        string pDes = Request.QueryString["pDes"];
                        string ppMin = Request.QueryString["ppMin"];
                        string ppMax = Request.QueryString["ppMax"];

                        if (pCode.Equals(""))
                            pCode = "~";
                        if (pName.Equals(""))
                            pName = "~";
                        if (pDes.Equals(""))
                            pDes = "~";
                        if (ppMin.Equals(""))
                            ppMin = "0";
                        if (ppMax.Equals(""))
                            ppMax = "0";
                        if (cId1.Equals("Default"))
                        {
                            var query1 = from p in dc.Products
                                         where p.pPrice >= Convert.ToInt32(ppMin) && p.pPrice <= Convert.ToInt32(ppMax)
                                         || SqlMethods.Like(p.pProductCode, "%" + pCode + "%")
                                         || SqlMethods.Like(p.pName, "%" + pName + "%")
                                         || SqlMethods.Like(p.pDescription, "%" + pDes + "%")
                                         select new { p.pActive, p.pBigImage, p.pCategoryId, p.pDescription, p.pId, p.pName, p.pPrice, p.pProductCode, p.pQuantity, p.pSmallImage };
                            MultiView1.ActiveViewIndex = 1;
                            ListView1.DataSource = query1;
                            ListView1.DataBind();
                        }
                        else
                        {

                            var query = from p in dc.Products
                                        join c in dc.Categories on p.pCategoryId equals c.cId
                                        where p.pPrice >= Convert.ToInt32(ppMin) && p.pPrice <= Convert.ToInt32(ppMax)
                                        || c.cId == Convert.ToInt32(cId1)
                                        || SqlMethods.Like(p.pProductCode, "%" + pCode + "%")
                                        || SqlMethods.Like(p.pName, "%" + pName + "%")
                                        || SqlMethods.Like(p.pDescription, "%" + pDes + "%")
                                        select new { p.pActive, p.pBigImage, p.pCategoryId, p.pDescription, p.pId, p.pName, p.pPrice, p.pProductCode, p.pQuantity, p.pSmallImage, c.cName };
                            MultiView1.ActiveViewIndex = 1;
                            ListView1.DataSource = query;
                            ListView1.DataBind();
                        }
                    }
                }
                else if (Request.QueryString["type"].Equals("3"))
                {
                    int cId1 = Convert.ToInt32(Request.QueryString["cId"]);
                    var query = from p in dc.Products
                                where p.pCategoryId == cId1
                                select p;
                    MultiView1.ActiveViewIndex = 1;
                    ListView1.DataSource = query;
                    ListView1.DataBind();
                }
                else
                {
                    string pName = Request.QueryString["pName"];
                    txtName.Text = pName;
                    var query = from p in dc.Products
                                join c in dc.Categories on p.pCategoryId equals c.cId
                                where SqlMethods.Like(p.pName, "%" + pName + "%")
                                select new { p.pActive, p.pBigImage, p.pCategoryId, p.pDescription, p.pId, p.pName, p.pPrice, p.pProductCode, p.pQuantity, p.pSmallImage, c.cName };
                    MultiView1.ActiveViewIndex = 1;
                    ListView1.DataSource = query;
                    ListView1.DataBind();
                }
            }


        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string type = "type=0&";
        string cId = "cId=" + ddlCate.SelectedValue + "&";
        string pCode = "pCode=" + txtCode.Text + "&";
        string pName = "pName=" + txtName.Text + "&";
        string pDes = "pDes=" + txtDes.Text + "&";
        string ppMin = "ppMin=" + txtPriceMin.Text + "&";
        string ppMax = "ppMax=" + txtPriceMax.Text;
        string pra = type + cId + pCode + pName + pDes + ppMin + ppMax;

        Response.Redirect("SreachResul.aspx?" + pra);


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    ShopDataContext ds = new ShopDataContext();
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Add"))
        {

            Basket b;
            BasketItem bi = new BasketItem();
            if (Session["MyCart"] != null)
            {
                b = Session["MyCart"] as Basket;
            }
            else
            {
                b = new Basket();
            }

            Label l = e.Item.Controls[0].FindControl("pProductCodeLabel") as Label;
            if (l != null)
            {

                Product p1 = ds.Products.FirstOrDefault(p => p.pProductCode == l.Text);
                bi.ProductCode = p1.pProductCode;
                bi.Quantity = 1;
                bi.Price = Convert.ToInt32(p1.pPrice);
                b.Add(bi);
                Session["MyCart"] = b;
            }
        }
    }
    protected void ddlCate_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}